package org.teamh.gui.ingame.effects;

import java.nio.FloatBuffer;

import org.teamh.gui.ingame.effects.Effect;
import org.teamh.logic.datamodel.Tower;
import static org.lwjgl.opengl.GL11.*;

public class TowerUpgradeProgress extends Effect{

	private Tower tower;
	private float posX;
	private float posY = 3f / 16;
	private float posZ;
	private float width = 1 / 16f;
	private float height = width / 4;
	
	private FloatBuffer modelviewMatrix;
	
	public TowerUpgradeProgress(String towerID) {
		Tower tower = getEffectManager().getIngameFunctions().getGui().getStart().getLogic().getManager().getTower(towerID);
		
		if(tower == null) {
			throw new NullPointerException("Tower " + towerID + " not found");
		}
		
		this.tower = tower;
		
		posX = tower.getCell().getWorldX() - width / 2;
		posY = 3 / 16f;
		posZ = tower.getCell().getWorldY();
		
		modelviewMatrix = getEffectManager().getIngameFunctions().getModelviewMatrix();
	}
	
	@Override
	public void render(float timeElapsed) {
		glDisable(GL_LIGHTING);
		glDisable(GL_TEXTURE_2D);
		if(!tower.isUpgrading()) {
			getEffectManager().removeEffect(this);
			getEffectManager().getIngameFunctions().getGui().getIngameScreen().getIngameScreenHUD().getController().updateTowerInfo(tower);
		}
		
		glBegin(GL_QUADS);
		{
			glColor3f(1, 0, 0);
			glVertex3d(posX, posY, posZ);
			glVertex3d(posX, posY + modelviewMatrix.get(5) * height, posZ + modelviewMatrix.get(9) * height);
			glVertex3d(posX + width, posY + modelviewMatrix.get(5) * height, posZ + modelviewMatrix.get(9) * height);
			glVertex3d(posX + width, posY, posZ);
			
			glColor3f(1, 1, 0);
			glVertex3d(posX, posY, posZ);
			glVertex3d(posX, posY + modelviewMatrix.get(5) * height, posZ + modelviewMatrix.get(9) * height);
			glVertex3d(posX + width * tower.getBuildProgress(), posY + modelviewMatrix.get(5) * height, posZ + modelviewMatrix.get(9) * height);
			glVertex3d(posX + width * tower.getBuildProgress(), posY, posZ);
		}
		glEnd();
		glColor3f(1, 1, 1);
	}
}
